package com.qf.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.device.dto.DeviceTypePageDto;
import com.qf.device.entity.DeviceType;
import com.qf.device.mapper.DeviceTypeMapper;
import com.qf.device.service.DeviceTypeService;
import com.qf.industry.entity.Industry;
import com.qf.industry.mapper.IndustryMapper;
import com.qf.industry.service.IndustryService;
import com.qf.industry.service.impl.IndustryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.security.DigestException;
import java.util.Date;
;

/**
 * <p>
 * 设备类型表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-10-26
 */
@Service
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceType> implements DeviceTypeService {


    @Override
    public PageResult<DeviceType> page(PageDto<DeviceTypePageDto> deviceTypePageDto) {
        IPage<DeviceType> page = new Page<>(deviceTypePageDto.getNow(), deviceTypePageDto.getSize());
        IPage<DeviceType> pageInfo = this.baseMapper.selectDeviceType(page, deviceTypePageDto.getWhere());
        PageResult<DeviceType> pageResult = new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setList(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Transactional
    @Override
    public R update(DeviceType deviceType) {
        deviceType.setUpdateTime(new Date());

        int i = baseMapper.updateById(deviceType);
        if (i > 0) {
            return R.ok("保存成功");
        } else {
            return R.fail("保存失败");
        }
    }


    @Override
    @Transactional
    public R add(DeviceType deviceType) {
        if (deviceType == null) {
            return R.fail();
        }
        String idStr = IdWorker.getIdStr();
        deviceType.setId(idStr);
        deviceType.setCreateTime(new Date());
        deviceType.setUpdateTime(new Date());
        this.baseMapper.insert(deviceType);
        return R.ok("设备添加成功");

    }

    @Override
    public R deleteDeviceType(String id) {
        int i = this.baseMapper.deleteById(id);
        if (i>0){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

}


